package com.ljb.main.v;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hikvision.vmsnetsdk.CameraInfo;
import com.hikvision.vmsnetsdk.ControlUnitInfo;
import com.hikvision.vmsnetsdk.LineInfo;
import com.hikvision.vmsnetsdk.RegionInfo;
import com.ljb.haikanglibrary.HaiKangManager;
import com.ljb.haikanglibrary.callbacks.MsgCallback;
import com.ljb.haikanglibrary.utils.JsonKit;
import com.ljb.haikanglibrary.utils.L;
import com.ljb.haikanglibrary.utils.SP;
import com.ljb.haikanglibrary.utils.ThreadUtils;
import com.ljb.main.DataTemp;
import com.ljb.main.R;
import com.ljb.main.RegionAdapter;

import java.util.List;


public class MainActivity extends BaseActivity {

    private Context c = this;
    private ListView lv_region;
    private List<RegionInfo> regionInfos;
    private RegionAdapter regionAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_region = (ListView) findViewById(R.id.lv_region);

        getData();
        String cameraInfoList_my = SP.getString(this, "cameraInfoList_my", null);
        if (cameraInfoList_my != null)
            DataTemp.init().cameraInfoList_my = new Gson().fromJson(cameraInfoList_my, new TypeToken<List<CameraInfo>>() {
            }.getType());
    }

    private void getData() {
        ThreadUtils.runInThread(new Runnable() {
            @Override
            public void run() {
                String mac = HaiKangManager.init().getMac(MainActivity.this);
                //1,获取分支线路
                List<LineInfo> lineInfos = HaiKangManager.init().fetchLines("http://61.235.138.54:82");

                //2,登录服务器
                HaiKangManager.init().loginServ("admin", "jkauto", lineInfos.get(0), mac);

                //3,请求控制中心列表
                HaiKangManager.init().requestCtrlUnitList(new MsgCallback() {
                    @Override
                    public void onMsg(int msgId, Object data) {
                        List<ControlUnitInfo> controlUnitInfos = (List<ControlUnitInfo>) data;
                        ControlUnitInfo controlUnitInfo = controlUnitInfos.get(0);

                        //4,请求区域列表
                        HaiKangManager.init().requestSubResFromCtrlUnit(controlUnitInfo.controlUnitID, new MsgCallback() {
                            @Override
                            public void onMsg(int msgId, Object data) {
                                regionInfos = (List<RegionInfo>) data;
                                L.e("区域列表:" + JsonKit.toJson(regionInfos));

                                //保存临时数据
                                DataTemp.init().regionInfoList = regionInfos;

                                //显示区域列表
                                showRegionList();
                            }
                        });
                    }
                });
            }
        });
    }

    /**
     * 显示区域列表
     */
    private void showRegionList() {
        ThreadUtils.runUIThread(new Runnable() {
            @Override
            public void run() {
                regionAdapter = new RegionAdapter(MainActivity.this, regionInfos);
                lv_region.setAdapter(regionAdapter);
                View item = MainActivity.this.getLayoutInflater().inflate(R.layout.item_region, null, false);
                TextView tv = (TextView) item.findViewById(R.id.tv_region);
                tv.setText("自定义区域");
                lv_region.addFooterView(item);

                //为区域列表设置点击事件
                lv_region.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        L.e("点击事件:position" + position);

                        if (position >= regionInfos.size()) {
                            L.e("自定义区域中的设备列表:" + JsonKit.toJson(DataTemp.init().cameraInfoList_my));
                            MonitorActivity.actionStart(c, -1);
                        } else {
                            L.e("region:" + regionAdapter.getItem(position).regionID);
                            MonitorActivity.actionStart(c, regionAdapter.getItem(position).regionID);
                        }
                    }
                });

                //为区域列表设置长按事件
                lv_region.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                        L.e("长按事件:position" + position);

                        if (position >= regionInfos.size()) {
                            longClick();
                        }
                        return true;
                    }
                });
            }
        });
    }


    /**
     * 长按事件
     */
    private void longClick() {
        new AlertDialog.Builder(c)
                .setTitle("编辑自定义区域")
                .setCancelable(true)
                .setNegativeButton("取消", null)
                .setPositiveButton("编辑", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //打开编辑自定义区域列表
                        SelectDeviceActivity.actionStart(c);
                    }
                })
                .show();
    }
}
